//logs.js
import {
  addAdress,
  delAdress,
  updateAdress
} from "../../utils/config";
import store from "../../utils/factory";

let that = null;

Page({
  data: {
    status: false,
    phone: "",
    address: "",
    name: "",
    submitStatus: false,
    delStatus: false,
    type: "",
    id: "",
    mode: "",
    current: 3,
    address2: ""
  },
  onLoad: function (e) {
    that = this;
    this.setData({
      type: e.type,
      mode: e.mode || ""
    });
    if (e.type == "edit") {
      let a = store.get("addressDetail");
      console.log("addressDetail", a)
      this.setData({
        id: a.id,
        phone: a.phone,
        name: a.name,
        current: a.type,
        status: a.defaultFlag
      });
      if (a.type == 2) {
        this.setData({
          address2: a.address
        })
      } else {
        this.setData({
          address: a.address
        })
      }
    }
    this.editTitle(e.type);
  },
  onShow() {
    if (store.get("delivery")) {
      this.setData({
        address: store.get("delivery"),
      });
      store.remove("delivery");
    }
  },
  switch1Change(e) {
    this.setData({
      status: e.detail.value
    })
    console.log(this.data.status)
  },
  editTitle(type) {
    wx.setNavigationBarTitle({
      title: type == "add" ? "新增收货地址" : "编辑收货地址"
    });
  },
  changeTab(e) {
    console.log(e)
    let active = e.currentTarget.dataset.active;
    that.setData({
      current: active
    })
  },
  bindRegionChange(e) {
    this.setData({});
  },
  inputAdress(e) {
    this.setData({
      address2: e.detail.value
    });
  },
  watchName(e) {
    this.setData({
      name: e.detail.value
    });
  },
  watchPhone(e) {
    this.setData({
      phone: e.detail.value
    });
  },
  watchAddress(e) {
    this.setData({
      address: e.detail.value
    });
  },
  check(e) {
    if (this.data.status) {
      this.setData({
        status: false
      });
    } else {
      this.setData({
        status: true
      });
    }
  },
  addAddress() {
    let address = "";
    let appdata = this.data;
    if (appdata.current == 3) {
      address = appdata.address
    } else {
      address = appdata.address2
    }
    addAdress({
      name: appdata.name,
      phone: appdata.phone,
      type: appdata.current,
      address: address,
      default: appdata.status
    }).then(res => {
      that.setData({
        submitStatus: false
      });
      wx.showModal({
        title: "温馨提示",
        content: "添加成功",
        showCancel: false, //是否显示取消按钮
        success: function (res) {
          wx.navigateBack({
            delta: 1
          });
        }
      });
    });
  },
  choiceAddress() {
    wx.navigateTo({
      url: "../delivery/delivery" // 页面 B
    });
  },
  editAddress() {
    let address = "";
    let appdata = this.data;
    if (appdata.current == 3) {
      address = appdata.address
    } else {
      address = appdata.address2
    }
    updateAdress({
      id: appdata.id,
      name: appdata.name,
      phone: appdata.phone,
      address: address,
      type: appdata.current,
      default: appdata.status
    }).then(res => {
      that.setData({
        submitStatus: false
      });
      wx.showModal({
        title: "温馨提示",
        content: "修改成功",
        showCancel: false, //是否显示取消按钮
        success: function (res) {
          wx.navigateBack({
            delta: 1
          });
        }
      });
    });
  },
  delAddress() {
    delAdress({
      id: this.data.id
    }).then(res => {
      that.setData({
        submitStatus: false
      });
      wx.showModal({
        title: "温馨提示",
        content: "删除成功",
        showCancel: false, //是否显示取消按钮
        success: function (res) {
          wx.navigateBack({
            delta: 1
          });
        }
      });
    });
  },
  save() {
    if (this.data.submitStatus) {
      return false;
    }
    this.setData({
      submitStatus: true
    });
    if (this.data.type == "add") {
      this.addAddress();
    } else {
      this.editAddress();
    }
  },
  del() {
    if (this.data.submitStatus) {
      return false;
    }
    this.setData({
      submitStatus: true
    });
    wx.showModal({
      title: "温馨提示",
      content: "是否确定删除该地址？",
      showCancel: true, //是否显示取消按钮
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定
          that.delAddress();
        }
      }
    });
  }
});